/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import static com.lineage.server.model.skill.L1SkillId.GREATER_HASTE;
import static com.lineage.server.model.skill.L1SkillId.HASTE;
import static com.lineage.server.model.skill.L1SkillId.STATUS_HASTE;

import com.lineage.server.model.L1Character;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.packetserver.S_SkillHaste;

/**
 * Class <code>Slow</code> 技能:缓速.
 * 法师魔法:缓速术 (20/0/1)
 * 在一定机率下藉由魔法锁链捆绑目标来降低1/3的行动速度
 * 
 * 法师魔法:集体缓速术 (20/0/2)
 * 将目标周围3格里的所有敌人施予缓速魔法，被施予集体缓速的敌人的移动速度会变慢。
 * 效果范围：3 x 3的矩形
 * 
 * 精灵魔法:地面障碍 (20/0/1)
 * 在持续时间，减少目标的移动速度
 * 
 * @author jrwz
 * @version 2012-7-26上午05:53:26
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class Slow extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new Slow();
    }

    /** 技能:缓速. */
    private Slow() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            final L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        if (cha instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) cha;
            if (pc.getHasteItemEquipped() > 0) {
                return;
            }
        }
        if (cha.getMoveSpeed() == 0) {
            if (cha instanceof L1PcInstance) {
                final L1PcInstance pc = (L1PcInstance) cha;
                pc.sendPackets(new S_SkillHaste(pc.getId(), 2,
                        getBuffIconDuration));
            }
            cha.broadcastPacket(new S_SkillHaste(cha.getId(), 2,
                    getBuffIconDuration));
            cha.setMoveSpeed(2);
        } else if (cha.getMoveSpeed() == 1) {
            int skillNum = 0;
            if (cha.hasSkillEffect(HASTE)) {
                skillNum = HASTE;
            } else if (cha.hasSkillEffect(GREATER_HASTE)) {
                skillNum = GREATER_HASTE;
            } else if (cha.hasSkillEffect(STATUS_HASTE)) {
                skillNum = STATUS_HASTE;
            }
            if (skillNum != 0) {
                cha.removeSkillEffect(skillNum);
                cha.removeSkillEffect(skillId);
                cha.setMoveSpeed(0);
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        if (cha instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) cha;
            pc.sendPackets(new S_SkillHaste(pc.getId(), 0, 0));
            pc.broadcastPacket(new S_SkillHaste(pc.getId(), 0, 0));
        }
        cha.setMoveSpeed(0);
    }
}
